/**
 * 
 */
package com.disys.common;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.disys.database.connectivity.DBConnectionManager;

/**
 * @author Ubaid Raja
 * 
 */
public class MetaData {
	public static HashMap<String, Integer> getMetaData(Connection connection, String tableName) {
//		Connection con;
//		con = DBConnectionManager.getConnection(DBConnectionManager.MYSQL);
		HashMap<String, Integer> sizeOfColumns = new HashMap<String, Integer>();
		if (connection != null) {
			try {
				ResultSet rsColumns = null;
				DatabaseMetaData meta = connection.getMetaData();
				rsColumns = meta
						.getColumns(null, null, tableName, null);
				while (rsColumns.next()) {
					String columnName = rsColumns.getString("COLUMN_NAME");
					int size = rsColumns.getInt("COLUMN_SIZE");
					sizeOfColumns.put(columnName, size);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return sizeOfColumns;
	}
	
	public static String appendSpaces(String data, Integer requiredLength) {
		StringBuffer newString = new StringBuffer(data);
		for (int i = data.length(); i < requiredLength; i++) {
			newString.append(' ');
		}
		return newString.toString();
	}
	
	public static String trailingSpaces(String data, Integer requiredLength) {
		StringBuffer newString = new StringBuffer();
		Integer difference = requiredLength - data.length();
		for (int i = 0; i < difference; i++) {
			newString.append(' ');
		}
		newString.append(data);
		return newString.toString();
	}
	
	public static void main(String[] args){
//		String data = "15027";
//		data = trailingSpaces(data, 15);
//		System.out.println(data);
//		
//		data = appendSpaces("", 75);
//		System.out.println(data);
		Connection con = DBConnectionManager
				.getConnection(DBConnectionManager.SQLSERVER);
		
		HashMap<String, Integer> sizeOfColumns = MetaData.getMetaData(con,
				"tx_202emplpim");
		
		Iterator it = sizeOfColumns.entrySet().iterator();
	    while (it.hasNext()) {
	        Map.Entry pairs = (Map.Entry)it.next();
	        System.out.println(pairs.getKey() + " = " + pairs.getValue());
	    }
	}
}
